﻿Imports BUS
Imports DTO
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Drawing
Imports System.IO
Public Class Export_Hoc_Sinh_Excel

    Private Sub Export_Hoc_Sinh_Excel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _Bus As New CHocSinhBus()
        Dim _ds As New List(Of CHocSinhDTO)
        _ds = _Bus.LayTatCaHS()
        Dim i As Integer
        For i = 0 To _ds.Count - 1
            dtgvdshs.Rows.Add(_ds(i).MaHS, _ds(i).HoTen, _ds(i).NgaySinh, _ds(i).Email, _ds(i).GioiTinh, _ds(i).DiaChi)
        Next
    End Sub

    Private Sub btnexport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexport.Click
        Dim _app As New Excel.Application()
        Dim _workbook As Excel.Workbook = _app.Workbooks.Add(Type.Missing)
        Dim _worksheet As New Excel.Worksheet()
        _app.Visible = True
        _worksheet = _workbook.Sheets(1)
        _worksheet.Name = "HocSinh"
        Dim i, j As Integer
        Dim _r As Excel.Range
        For j = 0 To dtgvdshs.Columns.Count - 1
            _r = _worksheet.Cells(1, j + 1)
            _r.Font.Color = ColorTranslator.ToOle(Color.Blue)
            _r.Font.Bold = True
            _r.ColumnWidth = 12
            _worksheet.Cells(1, j + 1) = dtgvdshs.Columns(j).HeaderText
        Next

        For i = 0 To dtgvdshs.Rows.Count - 2
            For j = 0 To dtgvdshs.Columns.Count - 1
                _worksheet.Cells(i + 2, j + 1) = dtgvdshs.Rows(i).Cells(j).Value
            Next
        Next
        Dim _dlg As New SaveFileDialog()
        _dlg.Filter = "|*.xlsx||*.xls"
        If _dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim _strpath As String = _dlg.FileName
            _workbook.SaveAs(_strpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        End If
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub
End Class